package ui;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.SwingUtilities;

import ui.table.EventButton;
import core.Controller;
import data.Event;


public class View implements PropertyChangeListener, Runnable, ActionListener
{
	private static Controller controllerObj;
	private static AppWindow window;
	private static ComponentMover cm;
	private static ResultBox resultBox;

	public View() {
		EventQueue.invokeLater(this);
		//createViews();
		try {
			GlobalKeyboardGrabber.setup();

			GlobalHotKeyListener listener = new GlobalHotKeyListener() {
				public void onHotKey() {
					if (window != null && window.isListAreaVisible()) {
						window.setListVisible(true);
					}
					window.showBoxOnScreen();
				}
			};
			GlobalKeyboardGrabber.addHotKeyListener(listener);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setController(Controller cont) {
		controllerObj = cont;
		//controllerObj.addPropertyChangeListener(this);
	}


	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		String changedProperty = evt.getPropertyName();
		if ("userCommand".equals(changedProperty)) {
			String userCommand = (String)evt.getNewValue();
			if("hide".equals(userCommand.trim())) {
				window.setBoxVisible(false);
				window.setListVisible(false);
			} else {
				controllerObj.setCommand(userCommand);
				SwingUtilities.invokeLater(controllerObj);
			}
		} else if ("clearView".equals(changedProperty)) {
			showOnlyBox();
		}
	}

	public void showEventsInList(Event[] resultEvents) {
		addtolist(resultEvents);
		window.setListVisible(true);
	}
	
	public void addtolist(Event[] resultEvents) {
		window.addEventsToList(resultEvents);   	  
	}

	public void run() {

		window = new AppWindow();
		window.setBoxVisible(true);
		window.setListVisible(false);
		window.setCommandListener(this);
		window.setParent(this);
		window.setButtonListener(this);
		cm = new ComponentMover();
		cm.registerComponent(window.getParentComponent());
		executeControllerCommand("reminder ");
		resultBox = new ResultBox(window);
	}	

	public void executeControllerCommand(String userCommand) {
		controllerObj.setCommand(userCommand);
		SwingUtilities.invokeLater(controllerObj);
	}

	public void unregisterProvider() {
		GlobalKeyboardGrabber.shutdown();
		window.getParentComponent().dispose();
	}

	public ResultBox displayResult(String result) {
		resultBox.setText(result);
		return resultBox;
	}

	public void showOnlyBox() {
		window.setOnlyBoxVisible();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof EventButton) {
			EventButton selectedButton = (EventButton) e.getSource();
			Event event = selectedButton.getEvent();
			String command = "toggle " + selectedButton.getType().name().toLowerCase() +" " +event.getId();
			executeControllerCommand(command);
		}
	}
	
	public void clearBoxText() {
		window.clearBoxText();
	}

	public void updateMultipleEventsInList(Event[] result) {
		window.updateMultipleEventsInLists(result);
	}
}